/**
 * Project Name:networkimageloader
 * Create User:yusr
 * Update User:yusr
 * Package Name:cn.cloudwalk.networkimageloader_03_with_diskcache.util
 * Date:2016/10/813:54
 * Copyright @ 2010-2016 Cloudwalk Information Technology Co.Ltd All Rights Reserved.
 */
package cn.cloudwalk.networkimageloader_03_with_diskcache.util;/**
 * Create User:yusr
 * Date:2016/10/813:54
 * Copyright @ 2010-2016 Cloudwalk Information Technology Co.Ltd All Rights Reserved.
 *
 */

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.lang.reflect.Field;

/**
 * ClassName: ImgUtil <br/>
 * Description: TODO Description. <br/>
 * date: 2016/10/8 13:54<br/>
 *
 * @author 284891377
 * @version v1.0.0
 * @since JDK 1.7
 */
public class ImgUtil {
    //TODO step1
    public static int[] getImageViewSize(ImageView imageView){
        int[] imgSizes=new int[2];
        DisplayMetrics displayMetrics=imageView.getContext().getResources().getDisplayMetrics();

        ViewGroup.LayoutParams lp=imageView.getLayoutParams();
        int width=imageView.getWidth();
        if(width<=0){
            width=lp.width;
        }
        if(width<=0){
            width=getImageViewFieldValue(imageView, "mMaxWidth");
        }
        if(width<=0){
            width=displayMetrics.widthPixels;
        }
        imgSizes[0]=width;
        int height = imageView.getHeight();// 获取imageview的实际高度
        if (height <= 0)
        {
            height = lp.height;// 获取imageview在layout中声明的宽度
        }
        if (height <= 0)
        {
            height = getImageViewFieldValue(imageView, "mMaxHeight");// 检查最大值
        }
        if (height <= 0)
        {
            height = displayMetrics.heightPixels;
        }
        imgSizes[1]=height;
        return imgSizes;
    }

    private static int getImageViewFieldValue(Object object, String fieldName) {
        int value = 0;
        try
        {
            Field field = ImageView.class.getDeclaredField(fieldName);
            field.setAccessible(true);
            int fieldValue = field.getInt(object);
            if (fieldValue > 0 && fieldValue < Integer.MAX_VALUE)
            {
                value = fieldValue;
            }
        } catch (Exception e)
        {
        }
        return value;
    }
    //TODO Step2 计算SampleSize
    /**
     * 根据需求的宽和高以及图片实际的宽和高计算SampleSize
     *
     * @param options
     * @param width
     * @param height
     * @return
     */
    public static int caculateInSampleSize(BitmapFactory.Options options, int reqWidth,
                                           int reqHeight)
    {
        int width = options.outWidth;
        int height = options.outHeight;

        int inSampleSize = 1;

        if (width > reqWidth || height > reqHeight)
        {
            int widthRadio = Math.round(width * 1.0f / reqWidth);
            int heightRadio = Math.round(height * 1.0f / reqHeight);

            inSampleSize = Math.max(widthRadio, heightRadio);
        }

        return inSampleSize;
    }

    /**
     * 根据图片需要显示的宽和高对图片进行压缩
     *
     * @param path
     * @param width
     * @param height
     * @return
     */
    public static  Bitmap decodeSampledBitmapFromPath(String path, int width,
                                                 int height)
    {
        // 获得图片的宽和高，并不把图片加载到内存中
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        options.inSampleSize = caculateInSampleSize(options,
                width, height);

        // 使用获得到的InSampleSize再次解析图片
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile(path, options);
        return bitmap;
    }
}